package com.mach.platform.sprider.dev;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class DevService {


    public static final String PLKEY = "_PL";
    public static final String VLKEY = "_VL";
    private static DevService me = new DevService();

    public static DevService get(){return DevService.me;}

    private Map<String, DataHelper> devsMap = new HashMap<String, DataHelper>();

    public DevService(){

        //压缩空气设备
        devsMap.put("1.0 总管流量", new CompessAirDev("192.168.3.90",8000,9,"1.0 总管流量"));
        devsMap.put("0.8 (12万) 总管流量", new CompessAirDev("192.168.3.90",8002,7,"0.8 (12万) 总管流量"));
        devsMap.put("0.8 (24万) 总管流量", new CompessAirDev("192.168.3.90",8002,8,"0.8 (24万) 总管流量"));
        devsMap.put("三线总管流量",new CompessAirDev("192.168.3.90",8001,3,"三线总管流量"));
        devsMap.put("PR2流量", new CompessAirDev("192.168.3.90",8001,5,"PR2流量"));
        devsMap.put("AF2流量", new CompessAirDev("192.168.3.90",8001,6,"AF2流量"));
        devsMap.put("WE2流量", new CompessAirDev("192.168.3.90",8001,14,"WE2流量"));
        devsMap.put("PA2流量", new CompessAirDev("192.168.3.90",8001,2,"PA2流量"));
        devsMap.put("PO2流量", new CompessAirDev("192.168.3.90",8001,4,"PO2流量"));
        devsMap.put("轮胎线流量", null);

        devsMap.put("1.0 总管露点", new LuDianKunHanDev("192.168.3.90",8004,1,"1.0 总管露点",1));
        devsMap.put("0.8 (12万) 总管露点", new LuDianKunHanDev("192.168.3.90",8004,1,"0.8 (12万) 总管露点",2));
        devsMap.put("0.8 (24万) 总管露点", new LuDianKunHanDev("192.168.3.90",8004,1,"0.8 (24万) 总管露点",3));
        //devsMap.put("三线总管露点", new LuDianKunHanDev("192.168.3.90",8004,1,"三线总管露点",4));


        //冷冻水
        devsMap.put("AF2车间", new CoolWaterDev("192.168.3.23",502,1,"AF2车间"));
        devsMap.put("PR2车间", new CoolWaterDev("192.168.3.21",502,1,"PR2车间"));
        devsMap.put("WE2车间", new CoolWaterDev("192.168.3.22",502,1,"WE2车间"));
        devsMap.put("PA2车间", new CoolWaterDev("192.168.3.26",502,1,"PA2车间"));
        devsMap.put("PO2车间", new CoolWaterDev("192.168.3.25",502,1,"PO2车间"));
        devsMap.put("PA2/PO2、24h车间", new CoolWaterDev("192.168.3.24",502,1,"PA2/PO2、24h车间"));

//        CONVERTER(new String[] {"1#变频器（AF2环境）", "2#变频器（AF2环境）", "4#变频器（PR2环境）",
//                "6#变频器（WE2环境）", "7#变频器（WE2环境）","8#变频器（PA2工艺16H）",
//                "9#变频器（PA2工艺16H）", "12#变频器（PO2工艺16H）", "14#变频器（PA2、PO2工艺24H）"}),

        //水泵状态:环境
        devsMap.put("1#变频器（AF2环境）1#泵", new WaterDumpKNDDev("192.168.3.92",8001,1,1,"1#变频器（AF2环境）1#泵"));
        devsMap.put("2#变频器（AF2环境）1#泵", new WaterDumpKNDDev("192.168.3.92",8002,3,1,"2#变频器（AF2环境）1#泵"));
        devsMap.put("2#变频器（AF2环境）2#泵", new WaterDumpKNDDev("192.168.3.92",8002,3,2,"2#变频器（AF2环境）2#泵",16));
        devsMap.put("4#变频器（PR2环境）1#泵", new WaterDumpKNDDev("192.168.3.92",8003,3,1,"4#变频器（PR2环境）1#泵",16));
        devsMap.put("6#变频器（WE2环境）1#泵", new WaterDumpKNDDev("192.168.3.92",8004,3,1,"6#变频器（WE2环境）1#泵"));
        devsMap.put("6#变频器（WE2环境）2#泵", new WaterDumpKNDDev("192.168.3.92",8004,3,2,"6#变频器（WE2环境）2#泵"));
        devsMap.put("7#变频器（WE2环境）1#泵", new WaterDumpKNDDev("192.168.3.92",8005,3,1,"7#变频器（WE2环境）1#泵"));
        devsMap.put("7#变频器（WE2环境）2#泵", new WaterDumpKNDDev("192.168.3.92",8005,3,2,"7#变频器（WE2环境）2#泵"));

        //水泵状态:工艺
        devsMap.put("8#变频器（PA2工艺16H）1#泵", new WaterDumpKNDDev("192.168.3.91",8000,4,1,"8#变频器（PA2工艺16H）1#泵"));
        devsMap.put("9#变频器（PA2工艺16H）1#泵", new WaterDumpKNDDev("192.168.3.91",8001,1,1,"9#变频器（PA2工艺16H）1#泵"));
        devsMap.put("9#变频器（PA2工艺16H）2#泵", new WaterDumpKNDDev("192.168.3.91",8001,1,2,"9#变频器（PA2工艺16H）2#泵"));
        devsMap.put("12#变频器（PO2工艺16H）1#泵", new WaterDumpKNDDev("192.168.3.91",8002,1,1,"12#变频器（PO2工艺16H）1#泵"));
        devsMap.put("14#变频器（PA2、PO2工艺24H）1#泵", new WaterDumpKNDDev("192.168.3.91",8003,1,1,"14#变频器（PA2、PO2工艺24H）1#泵"));

        //变频器:康耐德模块:抓取状态
        //环境
        devsMap.put("1#变频器（AF2环境）", new TransducerStatusKunHanDev("192.168.3.92",8001,2,"1#变频器（AF2环境）", (TransducerFaultStatusDev) devsMap.get("1#变频器（AF2环境）1#泵")));
        devsMap.put("2#变频器（AF2环境）", new TransducerStatusKunHanDev("192.168.3.92",8002,2,"2#变频器（AF2环境）", (TransducerFaultStatusDev) devsMap.get("2#变频器（AF2环境）2#泵")));
        devsMap.put("4#变频器（PR2环境）", new TransducerStatusKunHanDev("192.168.3.92",8003,2,"4#变频器（PR2环境）", (TransducerFaultStatusDev) devsMap.get("4#变频器（PR2环境）1#泵")));
        devsMap.put("6#变频器（WE2环境）", new TransducerSNDDev("192.168.3.92",8004,2,"6#变频器（WE2环境）"));
        devsMap.put("7#变频器（WE2环境）", new TransducerSNDDev("192.168.3.92",8009,2,"7#变频器（WE2环境）"));
        //工艺
        devsMap.put("8#变频器（PA2工艺16H）", new TransducerSNDDev("192.168.3.91",8000,5,"8#变频器（PA2工艺16H）"));
        devsMap.put("9#变频器（PA2工艺16H）", new TransducerSNDDev("192.168.3.91",8001,5,"9#变频器（PA2工艺16H）"));
        devsMap.put("12#变频器（PO2工艺16H）", new TransducerSNDDev("192.168.3.91",8002,5,"12#变频器（PO2工艺16H）"));
        devsMap.put("14#变频器（PA2、PO2工艺24H）", new TransducerStatusKunHanDev("192.168.3.91",8003,2,"14#变频器（PA2、PO2工艺24H）", (TransducerFaultStatusDev) devsMap.get("14#变频器（PA2、PO2工艺24H）1#泵")));


        //变频器:IMP930电脑模块:抓取电压，电流，功率
        //环境
        devsMap.put("1#变频器（AF2环境）" + VLKEY, new TransducerDBDev("192.168.3.92",8001,30,"1#变频器（AF2环境）" + VLKEY));
        devsMap.put("2#变频器（AF2环境）" + VLKEY, new TransducerDBDev("192.168.3.92",8002,30,"2#变频器（AF2环境）" + VLKEY));
        devsMap.put("4#变频器（PR2环境）" + VLKEY, new TransducerDBDev("192.168.3.92",8003,30,"4#变频器（PR2环境）" + VLKEY));
        devsMap.put("6#变频器（WE2环境）" + VLKEY, new TransducerDBDev("192.168.3.92",8004,30,"6#变频器（WE2环境）" + VLKEY));
        devsMap.put("7#变频器（WE2环境）" + VLKEY, new TransducerDBDev("192.168.3.92",8009,30,"7#变频器（WE2环境）" + VLKEY));
        //工艺
        devsMap.put("8#变频器（PA2工艺16H）" + VLKEY, new TransducerDBDev("192.168.3.91",8000,30,"8#变频器（PA2工艺16H）" + VLKEY));
        devsMap.put("9#变频器（PA2工艺16H）" + VLKEY, new TransducerDBDev("192.168.3.91",8001,30,"9#变频器（PA2工艺16H）" + VLKEY));
        devsMap.put("12#变频器（PO2工艺16H）" + VLKEY, new TransducerDBDev("192.168.3.91",8002,30,"12#变频器（PO2工艺16H）" + VLKEY));
        devsMap.put("14#变频器（PA2、PO2工艺24H）" + VLKEY, new TransducerDBDev("192.168.3.91",8003,30,"14#变频器（PA2、PO2工艺24H）" + VLKEY));



        //变频器:抓取频率
        //环境
        devsMap.put("1#变频器（AF2环境）" + PLKEY, new TransducerPlKunHanDev("192.168.3.92",8001,2,"1#变频器（AF2环境）" + PLKEY));
        devsMap.put("2#变频器（AF2环境）" + PLKEY, new TransducerPlKunHanDev("192.168.3.92",8002,2,"2#变频器（AF2环境）" + PLKEY));
        devsMap.put("4#变频器（PR2环境）" + PLKEY, new TransducerPlKunHanDev("192.168.3.92",8003,2,"4#变频器（PR2环境）" + PLKEY));
        devsMap.put("6#变频器（WE2环境）" + PLKEY, new TransducerPlSNDDev("192.168.3.92",8004,2,"6#变频器（WE2环境）" + PLKEY));
        devsMap.put("7#变频器（WE2环境）" + PLKEY, new TransducerPlSNDDev("192.168.3.92",8009,2,"7#变频器（WE2环境）" + PLKEY));
        //工艺
        devsMap.put("8#变频器（PA2工艺16H）" + PLKEY, new TransducerPlSNDDev("192.168.3.91",8000,5,"8#变频器（PA2工艺16H）" + PLKEY));
        devsMap.put("9#变频器（PA2工艺16H）" + PLKEY, new TransducerPlSNDDev("192.168.3.91",8001,5,"9#变频器（PA2工艺16H）" + PLKEY));
        devsMap.put("12#变频器（PO2工艺16H）" + PLKEY, new TransducerPlSNDDev("192.168.3.91",8002,5,"12#变频器（PO2工艺16H）" + PLKEY));
        devsMap.put("14#变频器（PA2、PO2工艺24H）" + PLKEY, new TransducerPlKunHanDev("192.168.3.91",8003,2,"14#变频器（PA2、PO2工艺24H）" + PLKEY));


    }




    /*
        冷冻水:供水温度
     */
    public Float getCoolWaterImportTemp(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(4, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        冷冻水:回水温度
     */
    public Float getCoolWaterBackTemp(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(40, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = ByteUtil.getFloat(data);

        return result;
    }
    /*
        冷冻水:累计流量
     */
    public Integer getCoolWaterSumFlow(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(8, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      冷冻水:累计冷量
   */
    public Integer getCoolWaterSumCool(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataTcp(12, 2);
        byte[] data = ByteUtil.subData(repData,9,4);
//        data = ByteUtil.reverse2Byte(data);
//        data = ByteUtil.reverse(data);
        result = ByteUtil.getInt(data);

        return result;
    }

    /*
      压缩空气：实时流量
     */
    public Float getCpAirRealTimeFlow(String devId) throws IOException {
        Float result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

//        byte[] repData = dh.getDataRtu(6,2);
//        byte[] data = ByteUtil.subData(repData,4,4);
//        data = ByteUtil.reverse2Byte(data);
//        result = ByteUtil.getFloat(data);

        byte[] repData = dh.getDataRtu(6,2);
        byte[] data = ByteUtil.subData(repData,3,4);
        ByteUtil.reveIn2Byte(data, 0);
        ByteUtil.reveIn2Byte(data, 2);
        result = ByteUtil.getFloat(data);

        return result;
    }

    /*
        压缩空气：累积流量
     */
    public Integer getCpAirSumFlow(String devId) throws IOException {
        Integer result = null;

        DataHelper dh = devsMap.get(devId);

        if(dh == null)return null;

        byte[] repData = dh.getDataRtu(12,2);
        byte[] data = ByteUtil.subData(repData,3,4);
        data = ByteUtil.reverse2Byte(data);
        data = ByteUtil.reverse(data);
        result = new Integer(ByteUtil.getInt(data));

        return result;
    }

    public Integer getTransducerStatus(String devName){
        GerenalStatusDev dh = (GerenalStatusDev)devsMap.get(devName);
        if(dh == null)return null;
        Integer rt = dh.getStatus();

        return rt;
    }

    public Float getTransducerPl(String devName){
        TransducerPlDev dh = (TransducerPlDev)devsMap.get(devName + PLKEY);

        if(dh == null)return null;
        Float rt = dh.getPl();

        return rt;
    }

    /*
    取变频器:电压
 */
    public Float getTransducerVolt(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;
        result = db.getVolt();


        return result;
    }

    /*
     取变频器:电流
  */
    public Float getTransducerElect(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;

        result = db.getElect();


        return result;
    }

    /*
     取变频器:功率
  */
    public Float getTransducerPower(String devId) {
        Float result = null;

        TransducerDBDev db = (TransducerDBDev)devsMap.get(devId + VLKEY);
        if(db == null)return null;
        result = db.getPower();


        return result;
    }

    /*
    取露点仪温度
 */
    public Float getLuDianTemp(String devId) {
        Float result = null;

        LuDianKunHanDev db = (LuDianKunHanDev)devsMap.get(devId);
        if(db == null)return null;
        result = db.getTemp();


        return result;
    }

    public void startDevDataReadService() throws IOException{

        Collection<DataHelper> devs = devsMap.values();

        for(DataHelper dh : devs){
            Thread th = new Thread(dh);
            th.start();
        }

    }



    public static void main(String[] args) throws IOException {
        get().startDevDataReadService();
    }

}


////压缩空气 名称
//$scope.compressedAirNames= [{
//        "name": '1.0 总管流量'}, {
//        "name": '0.8 (12万) 总管流量'}, {
//        "name": '0.8 (24万) 总管流量'}, {
//        "name": '三线总管流量'}, {
//        "name": 'PR2流量'}, {
//        "name": 'AF2流量'}, {
//        "name": 'WE2流量'}, {
//        "name": 'PA2流量'}, {
//        "name": 'PO2流量'}, {
//        "name": '轮胎线流量'}];
//
////冷冻水--首页--动态实时数据名称
//        $scope.coldWaterNames= [{
//        "name": "AF2车间"}, {
//        "name": "PR2车间"}, {
//        "name": "WE2车间"}, {
//        "name": "PA2车间"}, {
//        "name": "PO2车间"}, {
//        "name": "PA2/PO2、24h车间"}];
